﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Transactions;
using Tebbee.Business.Server.Model;

namespace Tebbee.Business.Server
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "AdvertiseSer" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select AdvertiseSer.svc or AdvertiseSer.svc.cs at the Solution Explorer and start debugging.
    public class AdvertiseSer : IAdvertiseSer
    {
        private readonly TebbeeContext context;

        public AdvertiseSer()
        {
            context = new TebbeeContext();
        }

        public ICollection<Advertise_GetByCatId_Result> Advertise_GetByCatId(string searchString, int currentPage, int catId, out int totalPage)
        {
            ObjectParameter ttPage = new ObjectParameter("TotalPage", typeof(Int32));
            var tmp = context.Advertise_GetByCatId(currentPage, searchString, catId, ttPage).ToList();
            totalPage = (Int32)ttPage.Value;
            return tmp;
        }

        public int Advertise_DeleteComment(int commentId)
        {
            ObjectParameter rowEffect = new ObjectParameter("RowEffected", typeof(Int32));
            context.Advertise_DeleteComment(commentId, rowEffect);
            return (Int32)rowEffect.Value;
        }

        public Advertise_ViewAdv_Result ViewAdv(string userName, int advId)
        {
            return context.Advertise_ViewAdv(userName, advId).Single();
        }

        public Advertise_Get_Result GetAdvertise(int id)
        {
            return context.Advertise_Get(id).Single();
        }

        public ICollection<Advertise_GetLink_Result> GetAdvertiseLink(int id, out string type)
        {
            ObjectParameter typeRT = new ObjectParameter("Type", typeof(string));
            var tmp = context.Advertise_GetLink(id, typeRT).ToList();
            type = (string) typeRT.Value;
            return tmp;
        }

        public ICollection<Advertise_TOP_Result> GetAdvertiseTop(string type)
        {
            return context.Advertise_TOP(type).ToList();
        }

        public ICollection<Advertise_Search_Result> Advertise_Search(string searchString)
        {
            return context.Advertise_Search(searchString).ToList();
        }


        public ICollection<Advertise_GetComment_Result> Advertise_GetComment(int id)
        {
            return context.Advertise_GetComment(id).ToList();
        }

        public void Advertise_Like(string userName, int advId)
        {
            context.Advertise_Like(advId, userName);
        }
        public void Advertise_Subcribe(string userName, int advId)
        {
            context.Advertise_Subcribe(advId, userName);
        }

        public void Advertise_Rate(string userName, int advId, double star)
        {
            context.Advertise_Rate(advId, userName, star);
        }

        public Advertise_SendComment_Result Advertise_SendComment(int id, string userName, string content)
        {
            return context.Advertise_SendComment(id, userName, content).Single();
        }

        public ICollection<Advertise_GetImage_Result> Advertise_GetImage(int id)
        {
            return context.Advertise_GetImage(id).ToList();
        }

        public ICollection<Advertise_GetMedia_Result> Advertise_GetMedia(int id)
        {
            return context.Advertise_GetMedia(id).ToList();
        }

        public int AddNewAdvertise(AdvertiseUpload adv)
        {
            ObjectParameter ReturnId = new ObjectParameter("ReturnId", typeof(Int32));
            int advId;
            using(var trans = new TransactionScope()){
                context.Advertise_Add(adv.Title,adv.ProductName, adv.Description, adv.PublisherId, adv.MediaTypeId,adv.PreviewImg,adv.ShortDes, ReturnId);
                advId = (Int32)ReturnId.Value;
                foreach (var item in adv.MediaLink)
                {
                    AddNewMedia(advId, item);
                }
                foreach (var item in adv.Categories)
                {
                    AddNewCatMap(advId, item);
                }
                trans.Complete();
            }
            return advId;
        }

        public int AddNewCatMap(int advId, int catId)
        {
            ObjectParameter ReturnId = new ObjectParameter("ReturnId", typeof(Int32));
            context.CatMap_Add(advId, catId, ReturnId);
            return (Int32)ReturnId.Value;
        }

        public int AddNewMedia(int advId, string mediaLink)
        {
            ObjectParameter ReturnId = new ObjectParameter("ReturnId", typeof(Int32));
            context.Media_Add(advId, mediaLink, ReturnId);
            return (Int32)ReturnId.Value;
        }


        public ICollection<Advertise_List_Result> ListAdvertising(int startRow, int elementQ, int orderBy, string searchString)
        {
            return context.Advertise_List(startRow,elementQ,orderBy, searchString).ToList();
        }

        public ICollection<CatMap_GetByAdv_Result> GetCategoriesByAdv(int advId)
        {
            ObjectParameter RowEffected = new ObjectParameter("RowEffected", typeof(Int32));
            return context.CatMap_GetByAdv(advId, RowEffected).ToList();
        }

        public ICollection<Media_GetByAdv_Result> GetMediaByAdv(int advId)
        {
            ObjectParameter RowEffected = new ObjectParameter("RowEffected", typeof(Int32));
            return context.Media_GetByAdv(advId, RowEffected).ToList();
        }


        public int DelAdv(int advId)
        {
            return context.Advertise_Delete(advId);
        }

        public int EditAdv(Advertisement adv)
        {
            return context.Advertise_Edit(adv.AdvId, adv.Title, adv.Description, adv.ShortDes, adv.ProductName);
        }


        public ICollection<Comment_GetByAdv_Result> GetCommentsByAdv(int advId)
        {
            return context.Comment_GetByAdv(advId).ToList();
        }


        public int EditComment(Comment cm)
        {
            ObjectParameter RowEffected = new ObjectParameter("RowEffected", typeof(Int32));
            return context.Comment_Edit(cm.CommentId, cm.Hide, cm.Spam, RowEffected);
        }



        public ICollection<string> GetDelImg(int advId)
        {
            return context.Advertise_GetDelImg(advId).ToList();
        }
    }
}
